﻿/*
=============================================================================
Author:魏宝辉通过CodeSmithe生成
CreatedTime:2012-10-21
Description:UserList.aspx.cs页面代码
=============================================================================	
*/

using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using FineUI;
using FTchina.DAL;
using FTchina.Library;
using FTchina.Logic;
using FTchina.Models;
using FTchina.SearchQuery;
using PWMIS.DataMap.Entity;
using System.Linq;
public partial class UserList_cs : BasePage
{




    string WindowsPage = "UserListaction.aspx";//弹出窗口的页面地址

    protected void Page_Load(object sender, EventArgs e)
    {


        if (!IsPostBack)
        {
            btnDelete.OnClientClick = Grid1.GetNoSelectionAlertReference("至少选择一项！");
            btnNew.OnClientClick = Window1.GetShowReference(WindowsPage, "新增");


            AddRootTree();//组织架构根目录


            BindGrid();



        }
    }

    #region 组织架构目录树
    /// <summary>
    /// 添加根项
    /// </summary>
    private void AddRootTree()
    {
        Tree1.Items.Clear();
        Tree1.Nodes.Clear();
        UserList ul = new UserList();
        OQLCompare cmp = new OQLCompare(ul);
        OQLCompare cmpresult = (cmp.Comparer(ul.ParentID, OQLCompare.CompareType.IS, "null"));
        OQL q = new OQL(ul);
        q.Select().Where(cmpresult).OrderBy(ul.Paixu, "ASC");
        List<UserList> UList = EntityQuery<UserList>.QueryList(q, ul);
        foreach (UserList item in UList)
        {
            TreeNode node = new TreeNode();
            node.Text = item.UName;
            node.NodeID = item.Id.ToString();
            node.AutoPostBack = true;
            node.EnablePostBack = true;
            node.Leaf = false;
            node.EnableCheckBox = false;
            Tree1.Nodes.Add(node);
        }
    }
    /// <summary>
    /// 展开树
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Tree1_NodeExpand(object sender, FineUI.TreeExpandEventArgs e)
    {
        DynamicAppendNode(e.Node);
    }


    /// <summary>
    /// 动态展开根项
    /// </summary>
    /// <param name="parentNode"></param>
    private void DynamicAppendNode(TreeNode parentNode)
    {

        parentNode.Expanded = true;

        UserList ul = new UserList();
        OQLCompare cmp = new OQLCompare(ul);
        OQLCompare cmpresult = (cmp.Comparer(ul.ParentID, OQLCompare.CompareType.Equal, parentNode.NodeID));

        cmpresult = cmpresult & cmp.Comparer(ul.IsDpt, OQLCompare.CompareType.Equal, "1");

        OQL q = new OQL(ul);
        q.Select().Where(cmpresult).OrderBy(ul.Paixu, "ASC");
        List<UserList> UList = EntityQuery<UserList>.QueryList(q, ul);

        foreach (UserList item in UList)
        {
            TreeNode node = new TreeNode();
            node.Text = item.UName;
            node.NodeID = item.Id.ToString();
            OQL qleave = new OQL(ul);
            qleave.Select().Where(cmp.Comparer(ul.ParentID, OQLCompare.CompareType.Equal, item.Id));
            List<UserList> UListLeave = EntityQuery<UserList>.QueryList(qleave, ul);
            node.Leaf = UListLeave.Count == 0;//是否子节点
            node.AutoPostBack = true;
            node.EnablePostBack = true;
            node.ToolTip = item.UName;
            parentNode.Nodes.Add(node);


        }
    }

    protected void Tree1_NodeCommand(object sender, FineUI.TreeCommandEventArgs e)
    {
        ttbSearch.Text = "";
        ttbSearch.ShowTrigger1 = false;
      
        BindGrid();
        // Alert.Show( "你点击了树节点：" + e.Node.Text);
    }

    protected void btnEditMenu_Click(object sender, EventArgs e)
    {
        if (Tree1.SelectedNodeID.Length == 0)
        {
            Alert.ShowInTop("请先选择一个部门");
        }
        else
        {
            PageContext.RegisterStartupScript(Window1.GetShowReference("UserListaction.aspx?Isdpt=1&id=" + Tree1.SelectedNodeID, "编辑部门"));
        }
        AddRootTree();//刷新列表
    }
    protected void btnAddMenu_Click(object sender, EventArgs e)
    {
        PageContext.RegisterStartupScript(Window1.GetShowReference("UserListaction.aspx?Isdpt=1", "新建部门"));
        AddRootTree();//刷新列表
    }
    protected void btnDelMenu_Click(object sender, EventArgs e)
    {
        if (Tree1.SelectedNodeID.Length == 0)
        {
            Alert.ShowInTop("请先选择一个部门");
            return;
        }

        if (UserListLogic.ChkIsSys(Tree1.SelectedNodeID))
        {
            AlertLogic.ShowError("此项为系统数据，不允许删除！");
            return;
        }

       

        if (Grid1.Rows.Count > 0)
        {
            Alert.ShowInTop("请先将部门内员工调走");
            return;
        }
        string ids = string.Empty;
        UserListLogic.getReferUserIds(Tree1.SelectedNodeID, ref ids);
        if (ids.Length > 0)
        {
            Alert.ShowInTop("请先将子部门删除");
            return;
        }

        UserListLogic.DeleteMenuItem(Tree1.SelectedNodeID);//删除部门及子部门及员工

        AddRootTree();//刷新列表

    }
    protected void btnReloadMenu_Click(object sender, EventArgs e)
    {
        AddRootTree();
    }
    #endregion


    #region 绑定列表
    private void BindGrid()
    {
        BindGrid(false);
    }
    private void BindGrid(bool IsToExcel)
    {
        UserList ul = new UserList();
        OQL q = new OQL(ul);
        OQLCompare cmp = new OQLCompare(ul);
        OQLCompare cmpResult = (
            cmp.Comparer(ul.IsDpt, OQLCompare.CompareType.Equal, "0")&(cmp.Comparer(ul.ParentID, OQLCompare.CompareType.Equal, Tree1.SelectedNodeID))
            );
        
        if (ttbSearch.Text.Trim().Length > 0)
        {
            cmpResult =  (cmp.Comparer(ul.UName, OQLCompare.CompareType.Like, "%" + ttbSearch.Text + "%")
                | cmp.Comparer(ul.UID, OQLCompare.CompareType.Like, "%" + ttbSearch.Text + "%"));
        }
        q.Select().Where(cmpResult).OrderBy(ul.Paixu, "ASC");

        List<UserList> uList = EntityQuery<UserList>.QueryList(q, ul);
        if (IsToExcel)
        {
            FTchina.OfficeHelper.ToExcel.IListToExcel<UserList>(uList);
            return;
        }
        Grid1.PageSize = 18;
        Grid1.RecordCount = uList.Count;
        Grid1.DataSource = uList.Skip<UserList>(Grid1.PageIndex * Grid1.PageSize).Take<UserList>(Grid1.PageSize);

        Grid1.DataBind();

    }


    /// <summary>
    /// 绑定表格
    /// </summary>
    //private void BindGrid(QueryModel qm, bool IsToExcel)
    //{


    //    DataTable dt = UserListLogic.getTreeDataTable();

    //    if (IsToExcel)
    //    {//如果是导出Excel
    //        FTchina.OfficeHelper.ToExcel.DataTableToExcel(dt);
    //        return;
    //    }

    //    //每页条数
    //    Grid1.PageSize = 2000;
    //    //总数
    //    Grid1.RecordCount = Convert.ToInt32(DBHelper.GetDataTable(qm.GetCountSql).Rows[0][0].ToString());//数据总条目数
    //    //分页数据源
    //    Grid1.DataSource = dt;
    //    Grid1.DataBind();

    //    if (qm.Key1.Length > 0)
    //    {
    //        ttbSearch.Text = qm.Key1;
    //        ttbSearch.ShowTrigger1 = true;
    //    }

    //}
    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Grid1_PageIndexChange(object sender, FineUI.GridPageEventArgs e)
    {

        Grid1.PageIndex = e.NewPageIndex;
        BindGrid();
    }
    /// <summary>
    /// 删除按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        foreach (int row in Grid1.SelectedRowIndexArray)
        {
            sb.Append(Grid1.DataKeys[row][0].ToString());
            sb.Append(",");
        }
        foreach (string ids in sb.ToString().TrimEnd(',').Split(','))
        {
            AlertLogic.ShowResult(UserListLogic.DeleteMenuItem(ids));
            // FTchina.BLL.UserGroupManager.DeleteUserGroupByid(Convert.ToInt32(ids));
        }
        BindGrid();
        Grid1.SelectedRowIndexArray = new int[] { };//去除默认的选项
    }


    /// <summary>
    /// 弹出窗口返回的处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Window1_Close(object sender, EventArgs e)
    {
        BindGrid();
    }
    #endregion

    #region 搜索过滤
    /// <summary>
    /// 点击搜索上的关闭按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ttbSearch_Trigger1Click(object sender, EventArgs e)
    {
        ttbSearch.ShowTrigger1 = false;
        ttbSearch.Text = String.Empty;//清空搜索字符串

        BindGrid();
    }
    /// <summary>
    /// 点击搜索按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ttbSearch_Trigger2Click(object sender, EventArgs e)
    {
        ttbSearch.ShowTrigger1 = true;//显示关闭搜索按钮
        BindGrid();
    }


    #endregion


    #region 导出Excel
    /// <summary>
    /// 带过滤条件的导出，不分页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnExcel_click(object sender, EventArgs e)
    {
        BindGrid(true);

    }
    #endregion


}






